﻿using System;
using System.Data;
using LibDBConnect;

namespace LibDataLayer
{
    public static class DalUsers
    {
        private static readonly SqlHelper Cls = new SqlHelper();
        #region[Get-Data]
        public static DataTable GetUser(string keywrods)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("KEYWORDS", keywrods);
            return Cls.GetData("sp_Users_Get");
        }
        public static DataTable GetUserEdit(string UserId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", UserId);
            return Cls.GetData("sp_Users_Get_Edit");
        }
        public static bool GetUserLogin(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserName", obj.UserName);
            Cls.AddParameter("Password", obj.Password);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Users_Get_Login", "@Msg");
            return true;
        }
        public static DataTable GetUserLoginAll(string UserName,string Passwords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserName", UserName);
            Cls.AddParameter("Password", Passwords);
            return Cls.GetData("sp_Users_Get_Login_All");
        }
        #endregion

        #region[Insert-Update-Delete]
        public static bool Insert(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", obj.UserId);
            Cls.AddParameter("UserName", obj.UserName);
            Cls.AddParameter("Email", obj.Email);
            Cls.AddParameter("Password", obj.Password);
            Cls.AddParameter("PasswordQuestion", obj.PasswordQuestion);
            Cls.AddParameter("PasswordAnswer", obj.PasswordAnswer);
            return Cls.ExecuteNonQuery("sp_User_Insert");
        }
        public static bool ChangePass(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", obj.UserId);
            Cls.AddParameter("Password", obj.Password);
            return Cls.ExecuteNonQuery("sp_User_ChangePass");
        }
        public static bool ChangeMail(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", obj.UserId);
            Cls.AddParameter("Email", obj.Email);
            return Cls.ExecuteNonQuery("sp_User_ChangeMail");
        }
        public static bool ChangeQuestion(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", obj.UserId);
            Cls.AddParameter("PasswordQuestion", obj.PasswordQuestion);
            Cls.AddParameter("PasswordAnswer", obj.PasswordAnswer);
            return Cls.ExecuteNonQuery("sp_User_ChangeQuestion");
        }
        public static bool Lock(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", obj.UserId);
            Cls.AddParameter("IsLockedOut", obj.IsLockedOut);
            return Cls.ExecuteNonQuery("sp_User_Udpate");
        }
        public static bool Delete(DTOUsers obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("UserId", obj.UserId);
            return Cls.ExecuteNonQuery("sp_User_Delete");
        }
        #endregion
    }

    public class DTOUsers
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public string ApplicationName { get; set; }
        public string Email { get; set; }
        public string Comment { get; set; }
        public string Password { get; set; }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public string Salt { get; set; }
        public bool IsApproved { get; set; }
        public DateTime LastActivityDate { get; set; }
        public DateTime LastUpdatedDate { get; set; }
        public DateTime LastLoginDate { get; set; }
        public DateTime LastPasswordChangeDate { get; set; }
        public DateTime CreationDate { get; set; }
        public bool IsOnLine { get; set; }
        public bool IsLockedOut { get; set; }
        public DateTime LastLockedOutDate { get; set; }
        public int ? FailedPasswordAttemptCount { get; set; }
        public DateTime FailedPasswordAttemptWindowStart { get; set; }
        public int ? FailedPasswordAnswerAttemptCount { get; set; }
        public DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }
        public int Msg { get; set; }
    }
}